package com.icetech.eom.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.icetech.eom.domain.sys.Custom;
import com.icetech.eom.dao.sys.CustomDao;
import com.icetech.eom.domain.tx.Server;
import com.icetech.eom.domain.tx.dto.CustomExcelDto;
import com.icetech.eom.domain.tx.dto.ServerExcelDto;
import com.icetech.eom.service.sys.ICustomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuxueli.poi.excel.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户管理 服务实现类
 * </p>
 *
 * @author wangzw
 * @since 2019-06-11
 */
@Service
public class CustomServiceImpl extends ServiceImpl<CustomDao, Custom> implements ICustomService {
    @Autowired
    private CustomDao customDao;
    @Override
    public PageInfo<Custom> selectByPage(String searchKey, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Custom> customs = customDao.selectByNameOrCode(searchKey);
        return new PageInfo(customs);
    }

    @Override
    public Workbook exportExcel(String searchKey) {
        List<Custom> customs = customDao.selectByNameOrCode(searchKey);
        List<CustomExcelDto> customExcelDtos = Lists.newArrayList();
        customs.forEach(custom -> {
            CustomExcelDto customExcelDto = new CustomExcelDto();
            BeanUtils.copyProperties(custom, customExcelDto);
            customExcelDtos.add(customExcelDto);
        });

        return ExcelExportUtil.exportWorkbook(customExcelDtos);
    }

    @Override
    public Custom selectByCode(String cusCode) {
        QueryWrapper<Custom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Custom::getCusCode,cusCode);
        return customDao.selectOne(queryWrapper);
    }
}
